const mysql = require('mysql');
const config = {
    database: 'notebook',
    user: 'root',
    password: 'remover',
    timezone: 'SYSTEM'
}
const TodoService = {
    getAllTodos: () => {
        sql = 'select * from t_todos;'
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('这是查到的待办事项', res);
                            resolve(res);
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    getDones: (bid) => {
        sql = 'select * from t_todos where bid = ? and isdone = 1 ;'
        let sqlParams = [bid];
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('这是查到的已完成待办事项', res);
                            resolve(res);
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    getUpDones: (bid) => {
        sql = 'select * from t_todos where bid = ? and isdone = 0 ;'
        let sqlParams = [bid];
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('这是查到的未完成待办事项', res);
                            resolve(res);
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    changeState: (tid, state) => {
        sql = 'update t_todos set isdone = ? where id = ? ;'
        let sqlParams = [state, tid];
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('这是更改待办事项状态的结果', res);
                            resolve(res);
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    newTodoByUid: (bid, date, content) => {
        let sqlParams = [bid, date, content];
        sql = 'insert into t_todos set  bid = ? ,date = ? ,content = ? ,isdone = 0 ;'
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('新增了' + res.length + '个待办');
                            console.log(res);
                            if (res.insertId > 0) {
                                resolve(1);
                            } else {
                                resolve(0);
                            }
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    deleteTodoByTid: (tid) => {
        let sqlParams = [tid];
        sql = 'delete from t_todos where id = ? ;'
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            if (res.affectedRows === 1) {
                                resolve(1);
                            } else {
                                resolve(0);
                            }
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    }
}
module.exports = TodoService